<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 200px;
            height: 200px;
            background-color: #6ff;
            margin: 50px;
            position: absolute;
            left: 50px;
            top: 50px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
</body>
<script>
    var box = document.querySelector(".box");

    // 改变元素的位置常用方法  margin 定位

    // 元素节点的属性 -> 元素偏移
    // offsetLeft   offsetTop    左偏移/上偏移 (margin和定位)
    console.log(box.offsetLeft, box.offsetTop);


    // 传统方法获取元素的偏移(此案例相对于父元素)
    // console.log(getStyle(box, "marginLeft"));
    // console.log(getStyle(box, "left"));

    // console.log(getVal(box, "marginLeft"));
    // console.log(getVal(box, "left"));


    function getVal(ele, key) {
        return parseFloat(getStyle(ele, key)); // 对getStyle的结果去单位
    }

    function getStyle(ele, key) {
        if (window.getComputedStyle) {
            return window.getComputedStyle(ele)[key];
        } else {
            return ele.currentStyle[key];
        }
    }


</script>

</html>